import Config from '../config';
import API from './util';

// 登录
const login = (data) => {
  return API.post('/wp-json/jwt-auth/v1/token', data)
}
// 获取主账号token 
const getAdminToken = () => {
  return API.post('/wp-json/jwt-auth/v1/token', {username: Config.ADMIN_ACCOUNT, password: Config.ADMIN_PASSWORD})
}
// 获取个人信息
const getUserInfo = (isShowToast = false) => {
  return API.get(`/wp-json/wp/v2/users/me`, {}, true, isShowToast)
}
// 更新用户个人信息
const updateUser = (data) => {
  return API.post(`/wp-json/wp/v2/users/me`, data, true)
}
// 注册
const register = (data) => {
  return API.post('/wp-json/wp/v2/users', data, true);
}
// 文章列表
const getArticleList = (data) => {
  return API.get('/wp-json/wp/v2/posts', data);
}
// 文章详情
const getArticlebyID = (id) => {
	return API.get('/wp-json/wp/v2/posts/'+id);   
}
// 文章发布
const addArticle = (data) => {
  return API.post('/wp-json/wp/v2/posts', data, true); 
}
// 分类列表
const getCategories = (data) => {
	return API.get('/wp-json/wp/v2/categories?orderby=id&order=asc', data);
}
// 分类信息
const getCategoryByID = (id) => {
	return API.get('/wp-json/wp/v2/categories/'+id);   
}
// 文章评论 
const getComments = (data, isToken?: boolean) => {
	return API.get('/wp-json/wp/v2/comments', data, isToken);
}
// 添加评论
const addComments = (data) => {
	return API.post('/wp-json/wp/v2/comments', data, true);
}
// 注销登录
const loginout = () => {
  my.removeStorageSync({key: 'user'});
  my.removeStorageSync({key: 'token'});
  console.log('退出登录');
  return true
} 
// 图片上传
const upload = (file) => {
  return API.uploadFile('/wp-json/wp/v2/media', file);
}

export default {
  login,
  register,
  getArticleList,
  getArticlebyID,
  getCategories,
  getCategoryByID,
  getComments,
  addComments,
  getUserInfo,
  updateUser,
  loginout,
  addArticle,
  upload,
  getAdminToken
}
